package org.he.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.he.basic.annotation.HePermission;
import org.he.basic.util.AjaxResult;
import org.he.basic.util.PageInfo;
import org.he.org.domain.Department;
import org.he.org.query.DepartmentQuery;
import org.he.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author He
 * @date 2023/11/28  14:40
 */
@RestController
@RequestMapping("/department")
@Api(value = "部门管理Controller",description = "部门的增删查改接口")
@HePermission(name = "部门管理",desc = "管理部门的接口" )
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    // 增加和修改一起 put
    @PutMapping
//    @ResponseBody      不管是put请求还是post请求,这里要写@RequestBody
    @ApiOperation(value = "增加或修改部门")
    @HePermission(name = "部门新增和修改", desc = "部门新增和修改")
    public AjaxResult save(@RequestBody Department department) {
        try {
            if (Objects.isNull(department.getId())) {
                departmentService.insert(department);
            } else {
                departmentService.update(department);
            }
            return AjaxResult.success(department, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败");
        }

    }

    // 删除 delete
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除部门")
    @HePermission(name = "部门删除", desc = "部门删除")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            departmentService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    // 查询get
    // 查一个
    @GetMapping("/{id}")
    @ApiOperation(value = "查询指定部门")
    @HePermission(name = "查询指定部门", desc = "查询指定部门")
    public AjaxResult queryById(@PathVariable("id") Long id) {
        try {
            Department department = departmentService.queryById(id);
            return AjaxResult.success(department, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    // 查全部
    @GetMapping
    @ApiOperation(value = "查询全部部门")
    @HePermission(name = "查询全部部门", desc = "查询全部部门")
    public AjaxResult queryAll(){
        try {
            List<Department> departments = departmentService.queryAll();
            return AjaxResult.success(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @PostMapping
    @ApiOperation(value = "分页接口") // 不管是put请求还是post请求,这里要写@RequestBody
    @HePermission(name = "部门分页查询", desc = "部门分页查询")
    public AjaxResult queryPage(@RequestBody DepartmentQuery departmentQuery){
        try {
            PageInfo<Department> page = departmentService.queryPage(departmentQuery);
            return AjaxResult.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除接口") // 不管是put请求还是post请求,还有patch请求这里要写@RequestBody
    @HePermission(name = "批量删除部门", desc = "批量删除部门")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    // 查询一级二级部门
    @GetMapping("/tree")
    @HePermission(name = "查询部门树", desc = "查询部门树")
    public AjaxResult getTree(){
        try {
            List<Department> departments = departmentService.getTree();
            return AjaxResult.success(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
}
